<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="detailNav"></detail-nav-bar>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info ref="params" :param-info="paramInfo"></detail-param-info>
      <detail-comment-info ref="comment" :comment-info="commentInfo"></detail-comment-info>
      <goods-list ref="recommend" :goods="recommends"></goods-list>
    </scroll>
    <detail-bottom-nav @addToCart="addToCart"></detail-bottom-nav>
    <back-top @click.native="backTopClick" v-show="isShowBackTop"></back-top>
    <!--提示信息-->
    <!--    <toast :message="addCartMessage" :is-show="isMessageShow"></toast>-->
  </div>
</template>

<script>
import Scroll from "@/components/common/scroll/Scroll";
import DetailNavBar from "@/views/detail/childComps/DetailNavBar";
import DetailSwiper from "@/views/detail/childComps/DetailSwiper";
import DetailBaseInfo from "@/views/detail/childComps/DetailBaseInfo";
import DetailShopInfo from "@/views/detail/childComps/DetailShopInfo";
import DetailGoodsInfo from "@/views/detail/childComps/DetailGoodsInfo";
import DetailParamInfo from "@/views/detail/childComps/DetailParamInfo";
import DetailCommentInfo from "@/views/detail/childComps/DetailCommentIofo";
import DetailBottomNav from "@/views/detail/childComps/DetailBottomNav";
import GoodsList from "@/components/content/goods/GoodsList";
import {getDetail, getRecommend, Goods, Shop, GoodsParam} from "@/network/detail";
import {itemListenerMixin, backTopMixin} from "@/common/mixin";
import Toast from "@/components/common/toast/Toast";

export default {
  name: "Detail",
  components: {
    Toast,
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomNav,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],//各个板块距top的位置
      currentIndex: 0,//当前用户浏览的板块
      addCartMessage: '',
      isMessageShow: false,
    }
  },
  created() {
    this.iid = this.$route.params.iid;
    //根据iid查询商品详情数据
    getDetail(this.iid).then(res => {
      const data = res.result;
      this.topImages = data.itemInfo.topImages;
      //获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      //获取店铺信息
      this.shop = new Shop(data.shopInfo)
      //保存商品详情数据
      this.detailInfo = data.detailInfo;
      //获取商品参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
      //获取评论信息
      if (data.rate.list) {
        this.commentInfo = data.rate.list[0];
      }
    });

    //获取推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.list;
    })
    //页面渲染完毕后回调函数  下一帧？
    /*this.nextTick(()=>{});*/
  },
  //使用混入，所以注释mounted()
  mixins: [itemListenerMixin, backTopMixin],
  /*mounted() {
    //监听item中图片加载完成
    const refresh = debounce(this.$refs.scroll.refresh, 100)
    this.itemImageLoad = () => {//重新计算可滚动区域
      refresh();
    }
    this.$bus.$on('itemImageLoad', this.itemImageLoad);
  },*/
  destroyed() {
    //页面销毁时取消全局事件的监听 （因为没有使用keep-alive 所以没有活跃与不活跃的状态）
    this.$bus.$off('itemImageLoad', this.itemImageLoad)
  },
  methods: {
    imageLoad() {
      if (this.themeTopYs.length === 0) {
        //获取三个板块的y轴值
        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
        this.themeTopYs.push(Number.MAX_VALUE);
      }
      this.$refs.scroll.refresh();
    },
    //标题切换，页面随之滚动到响应的位置
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
    },
    //监听界面的滚动
    contentScroll(position) {
      const themeTopY = this.themeTopYs;
      let positionY = -position.y;
      for (let i = 0; i < themeTopY.length - 1; i++) {
        //根据当前页面的y轴值，判断当前用户在浏览那个板块##普通方法
        /*if (this.currentIndex !== i
          && ((i < themeTopY.length - 1 && positionY >= themeTopY[i] && positionY < themeTopY[i + 1])
            || (i === themeTopY.length - 1 && positionY >= themeTopY[i]))) {
          this.currentIndex = i;
          this.$refs.detailNav.currentIndex = i;
          break;
        }*/
        //特殊方法,给数组最后一个数据后面加一个更大的值
        //Number.MAX_VALUE   //JS中Numbar的最大值
        if (this.currentIndex !== i && (positionY >= themeTopY[i] && positionY < themeTopY[i + 1])) {
          this.currentIndex = i;
          this.$refs.detailNav.currentIndex = i;
          break;
        }
      }
      //监听回到顶部按钮是否显示
      this.listenBackTopIsShow(position);
    },
    //添加购物车
    addToCart() {
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      //将商品添加到购物车
      //this.$store.commit('addCart', product);
      this.$store.dispatch('addCart', product).then(res => {
        //添加成功，显示提示信息
        /*this.isMessageShow = true;
        this.addCartMessage = res;
        setTimeout(()=>{
          this.isMessageShow = false
        },3000)*/
        this.$toast.msg(res);
      });
    },

  },
}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content {
  height: calc(100vh - 44px);

  overflow: hidden;
}
</style>
